package com.ruoyi.web.controller.config;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ViewRelationMetadata;
import com.ruoyi.system.service.ViewRelationMetadataService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (ViewRelationMetadata)表控制层
 *
 * @author makejava
 * @since 2022-12-16 20:23:52
 */
@RestController
@RequestMapping("view/relation/metadata")
public class ViewRelationMetadataController {
    /**
     * 服务对象
     */
    @Resource
    private ViewRelationMetadataService viewRelationMetadataService;

    /**
     * 分页查询
     *
     * @param viewRelationMetadata 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<ViewRelationMetadata>> queryByPage(ViewRelationMetadata viewRelationMetadata, PageRequest pageRequest) {
        return ResponseEntity.ok(this.viewRelationMetadataService.queryByPage(viewRelationMetadata, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<ViewRelationMetadata> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.viewRelationMetadataService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param viewRelationMetadata 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public AjaxResult add(@Validated @RequestBody ViewRelationMetadata viewRelationMetadata) {
        //基本数据校验;
        if (StringUtils.isEmpty(viewRelationMetadata.getConfViewName())) {
            return AjaxResult.error("配置类型不可为空！");
        }
        if (null == viewRelationMetadata.getFileId()) {
            return AjaxResult.error("文件ID不能为空！");
        }
        if (StringUtils.isEmpty(viewRelationMetadata.getMetadataId())) {
            return AjaxResult.error("元数据ID不可为空！");
        }
        this.viewRelationMetadataService.insert(viewRelationMetadata);
        return AjaxResult.success();
    }

    /**
     * 编辑数据
     *
     * @param viewRelationMetadata 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<ViewRelationMetadata> edit(ViewRelationMetadata viewRelationMetadata) {
        return ResponseEntity.ok(this.viewRelationMetadataService.update(viewRelationMetadata));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.viewRelationMetadataService.deleteById(id));
    }

}

